# 1qt - bundle Qt frameworks and PyQt
# ! cmakecache vars not available to external scripts
#   so we configure it first to do substitutions
#   make sure to use @varname@

# kill boolean warnings
CMAKE_POLICY (SET CMP0012 NEW)

INCLUDE ("@CMAKE_BINARY_DIR@/mac/0vars.cmake")
INCLUDE ("@CMAKE_SOURCE_DIR@/cmake/MacBundleMacros.cmake")
INCLUDE ("@CMAKE_SOURCE_DIR@/cmake/QCAMacros.cmake")

# Qt framework version is major version
SET (QT_FWVER "5")

# build list of Qt frameworks to bundle

# core list, includes dependencies and used by extra plugins
SET (QTLISTQG QtCore QtGui QtWidgets QtNetwork QtXml QtSvg QtConcurrent QtPrintSupport QtSerialPort QtPositioning QtTest QtSql QtDBus)
SET (PYQTLIST Qt QtCore QtGui QtWidgets QtNetwork QtXml QtSvg QtPrintSupport QtPositioning QtSerialPort QtTest QtSql QtDBus)

# QtQuickWidgets appears to be implied direct dep, it needs Quick and Qml,
# whether or not WITH_QUICK specified
SET (QTLISTQG ${QTLISTQG} QtQuickWidgets QtQuick QtQml)
SET (PYQTLIST ${PYQTLIST} QtQuickWidgets QtQuick QtQml)

IF(@WITH_QTWEBKIT@)
    SET (QTLISTQG ${QTLISTQG} QtWebKit QtWebKitWidgets QtSensors QtWebChannel QtMultimedia QtMultimediaWidgets QtOpenGL)
    SET (PYQTLIST ${PYQTLIST} QtWebKit QtWebKitWidgets QtSensors QtWebChannel QtMultimedia QtMultimediaWidgets QtOpenGL)
ENDIF ()

IF(@WITH_3D@)
    SET (QTLISTQG ${QTLISTQG} Qt3DCore Qt3DRender Qt3DInput Qt3DLogic Qt3DExtras QtGamepad)
    SET (PYQTLIST ${PYQTLIST} Qt3DCore Qt3DRender Qt3DInput Qt3DLogic Qt3DExtras QtGamepad)
ENDIF ()

# add Qsci.so, if available
IF (@QSCI_FOUND@)
    SET (PYQTLIST ${PYQTLIST} Qsci)
ENDIF (@QSCI_FOUND@)

MYMESSAGE ("Qt list: ${QTLISTQG}")

# symlinks when only @executable_path used

EXECUTE_PROCESS (COMMAND ln -sfn ../Frameworks "${QAPPDIR}/")
IF (IS_DIRECTORY "${QLIBDIR}/grass/bin")
    EXECUTE_PROCESS (COMMAND ln -sfn ../@QGIS_LIBEXEC_SUBDIR_REV@/@QGIS_FW_SUBDIR@ "${QLIBDIR}/grass/Frameworks")
ENDIF ()
EXECUTE_PROCESS (COMMAND ln -sfn @QGIS_CGIBIN_SUBDIR_REV@/@QGIS_LIB_SUBDIR@ "${QCGIDIR}/lib")

### copy files & strip qt rpath

# Qt frameworks
#   Qt5 cmake does not create overall qt prefix var, only individual lib prefixes.
#   For simplicity, assume core prefix same as all others.

MESSAGE (STATUS "Copying Qt frameworks...")
EXECUTE_PROCESS (COMMAND mkdir -p "${QFWDIR}")
FOREACH (QFW ${QTLISTQG})
    IF (NOT IS_DIRECTORY "${QFWDIR}/${QFW}.framework")
        COPY_FRAMEWORK("${QT_LIBRARY_DIR}" ${QFW} "${QFWDIR}")
        EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QFWDIR}/${QFW}.framework/${QFW}")
    ENDIF ()
ENDFOREACH (QFW)

# Qt plugins

EXECUTE_PROCESS (COMMAND mkdir -p "${QPLUGDIR}/../imageformats")
FOREACH (QI qgif;qico;qjpeg;qsvg;qtiff)
    IF (NOT EXISTS "${QPLUGDIR}/../imageformats/lib${QI}.dylib")
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QT_PLUGINS_DIR@/imageformats/lib${QI}.dylib" "${QPLUGDIR}/../imageformats/lib${QI}.dylib")
        EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QPLUGDIR}/../imageformats/lib${QI}.dylib")
    ENDIF ()
ENDFOREACH (QI)
EXECUTE_PROCESS (COMMAND mkdir -p "${QPLUGDIR}/../platforms")
FOREACH (QTP cocoa;minimal;offscreen)
    IF (NOT EXISTS "${QPLUGDIR}/../platforms/libq${QTP}.dylib")
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QT_PLUGINS_DIR@/platforms/libq${QTP}.dylib" "${QPLUGDIR}/../platforms/libq${QTP}.dylib")
        EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QPLUGDIR}/../platforms/libq${QTP}.dylib")
    ENDIF ()
ENDFOREACH (QTP)
EXECUTE_PROCESS (COMMAND mkdir -p "${QPLUGDIR}/../sqldrivers")
SET (QTLISTSQL ite odbc psql mysql)
# qspatialite driver plugin is part of QGIS build
IF (@WITH_QSPATIALITE@)
    LIST(APPEND QTLISTSQL spatialite)
    IF (EXISTS "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
        EXECUTE_PROCESS (COMMAND ${CMAKE_COMMAND} -E remove "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
        EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
    ENDIF ()
ENDIF ()
FOREACH (QSL ${QTLISTSQL})
    IF (NOT EXISTS "${QPLUGDIR}/../sqldrivers/libqsql${QSL}.dylib" AND EXISTS "@QT_PLUGINS_DIR@/sqldrivers/libqsql${QSL}.dylib")
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QT_PLUGINS_DIR@/sqldrivers/libqsql${QSL}.dylib" "${QPLUGDIR}/../sqldrivers/libqsql${QSL}.dylib")
        EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QPLUGDIR}/../sqldrivers/libqsql${QSL}.dylib")
    ENDIF ()
ENDFOREACH (QSL)
IF (NOT @WITH_QSPATIALITE@ AND EXISTS "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
    EXECUTE_PROCESS (COMMAND ${CMAKE_COMMAND} -E remove "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
ENDIF ()
EXECUTE_PROCESS (COMMAND mkdir -p "${QPLUGDIR}/../iconengines")
IF (NOT EXISTS "${QPLUGDIR}/../iconengines/libqsvgicon.dylib")
    EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QT_PLUGINS_DIR@/iconengines/libqsvgicon.dylib" "${QPLUGDIR}/../iconengines/libqsvgicon.dylib")
    EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QPLUGDIR}/../iconengines/libqsvgicon.dylib")
ENDIF ()
EXECUTE_PROCESS (COMMAND mkdir -p "${QPLUGDIR}/../styles")
IF (NOT EXISTS "${QPLUGDIR}/../styles/libqmacstyle.dylib")
    EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QT_PLUGINS_DIR@/styles/libqmacstyle.dylib" "${QPLUGDIR}/../styles/libqmacstyle.dylib")
    EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QPLUGDIR}/../styles/libqmacstyle.dylib")
ENDIF ()

# Qwt

STRING (REGEX MATCH "\\.dylib$" QWT_ISLIB "@QWT_LIBRARY@")
STRING (REGEX MATCH "qwt.framework" QWT_ISFW "@QWT_LIBRARY@")
IF (QWT_ISLIB)
    # shared libraries
    MESSAGE (STATUS "Copying Qwt and updating library paths...")
    IF (NOT EXISTS "${QLIBDIR}/libqwt.dylib")
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QWT_LIBRARY@" "${QLIBDIR}/libqwt.dylib")
        EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QLIBDIR}/libqwt.dylib")
    ENDIF ()
    GET_INSTALL_NAME ("@QWT_LIBRARY@" "libqwt" QWT_CHG)
    IF (QWT_CHG)
        UPDATEQGISPATHS (${QWT_CHG} libqwt.dylib)
    ENDIF (QWT_CHG)
ELSEIF (QWT_ISFW AND EXISTS "@QWT_LIBRARY@")
    # framework
    MESSAGE (STATUS "Copying Qwt framework and updating library paths...")
    STRING(REGEX REPLACE "/qwt.framework.*" "" QWT_PARENT "@QWT_LIBRARY@")
    IF (NOT IS_DIRECTORY "${QFWDIR}/qwt.framework")
        COPY_FRAMEWORK("${QWT_PARENT}" "qwt" "${QFWDIR}")
        EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QFWDIR}/qwt.framework/qwt")
    ENDIF ()
    GET_INSTALL_NAME ("${QWT_PARENT}/qwt.framework/qwt" "qwt.framework" QWT_CHG)
    IF (QWT_CHG)
        UPDATEQGISPATHS (${QWT_CHG} qwt)
    ENDIF (QWT_CHG)
ENDIF (QWT_ISLIB)


# QwtPolar

IF (@WITH_DESKTOP@)
  IF (@WITH_QWTPOLAR@ AND NOT @WITH_INTERNAL_QWTPOLAR@)
    STRING (REGEX MATCH "\\.dylib$" ISLIB "@QWTPOLAR_LIBRARY@")
    STRING (REGEX MATCH "qwtpolar.framework" ISFW "@QWTPOLAR_LIBRARY@")
    IF (ISLIB)
        # shared libraries
        MESSAGE (STATUS "Copying QwtPolar and updating library paths...")
        IF (NOT EXISTS "${QLIBDIR}/libqwtpolar.dylib")
            EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QWT_LIBRARY@" "${QLIBDIR}/libqwtpolar.dylib")
            EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QLIBDIR}/libqwtpolar.dylib")
        ENDIF ()
        GET_INSTALL_NAME ("@QWTPOLAR_LIBRARY@" "libqwtpolar" QWTP_CHG)
        IF (QWTP_CHG)
            UPDATEQGISPATHS (${QWTP_CHG} libqwtpolar.dylib)
        ENDIF (QWTP_CHG)
        # update qwt lib in qwtpolar
        IF (QWT_CHG)
            IF (QWT_ISLIB)
                SET (QWT_CHG_TO "${ATLOADER}/libqwt.dylib")
            ElSE ()
                SET (QWT_CHG_TO "${ATLOADER}/${QGIS_LIB_SUBDIR_REV}/${QGIS_FW_SUBDIR}/qwt.framework/qwt")
            ENDIF ()
            INSTALLNAMETOOL_CHANGE ("${QWT_CHG}" "${QWT_CHG_TO}" "${QLIBDIR}/libqwtpolar.dylib")
        ENDIF (QWT_CHG)
    ELSEIF (ISFW AND EXISTS "@QWTPOLAR_LIBRARY@")
        # framework
        MESSAGE (STATUS "Copying QwtPolar framework and updating library paths...")
        STRING(REGEX REPLACE "/qwtpolar.framework.*" "" QWTP_PARENT "@QWTPOLAR_LIBRARY@")
        IF (NOT IS_DIRECTORY "${QFWDIR}/qwtpolar.framework")
            COPY_FRAMEWORK("${QWTP_PARENT}" "qwtpolar" "${QFWDIR}")
            EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QFWDIR}/qwtpolar.framework/qwtpolar")
        ENDIF ()
        GET_INSTALL_NAME ("${QWTP_PARENT}/qwtpolar.framework/qwtpolar" "qwtpolar.framework" QWTP_CHG)
        IF (QWTP_CHG)
            UPDATEQGISPATHS (${QWTP_CHG} qwtpolar)
        ENDIF (QWTP_CHG)
        # update qwt lib in qwtpolar
        IF (QWT_CHG)
            IF (QWT_ISLIB)
                SET (QWT_CHG_TO "${ATLOADER}/../../../${QGIS_FW_SUBDIR_REV}/${QGIS_LIB_SUBDIR}/libqwt.dylib")
            ElSE ()
                SET (QWT_CHG_TO "${ATLOADER}/../../../qwt.framework/qwt")
            ENDIF ()
            INSTALLNAMETOOL_CHANGE ("${QWT_CHG}" "${QWT_CHG_TO}" "${QFWDIR}/qwtpolar.framework/qwtpolar")
        ENDIF (QWT_CHG)
    ENDIF (ISLIB)
  ENDIF (@WITH_QWTPOLAR@ AND NOT @WITH_INTERNAL_QWTPOLAR@)
ENDIF (@WITH_DESKTOP@)

# QCA

STRING (REGEX MATCH "\\.dylib$" ISLIB "@QCA_LIBRARY@")
STRING (REGEX MATCH "qca.*.framework" ISFW "@QCA_LIBRARY@")
IF (ISLIB)
    # shared libraries
    MESSAGE (STATUS "Copying QCA and updating library paths...")
    SET (QCA_CHG_TO "${ATLOADER}/@QGIS_PLUGIN_SUBDIR_REV@/${QGIS_LIB_SUBDIR}/libqca.dylib")
    IF (NOT EXISTS "${QLIBDIR}/libqca.dylib")
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QCA_LIBRARY@" "${QLIBDIR}/libqca.dylib")
        EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QLIBDIR}/libqca.dylib")
    ENDIF ()
    GET_INSTALL_NAME ("@QCA_LIBRARY@" "libqca" QCA_CHG)
    IF (QCA_CHG)
        UPDATEQGISPATHS (${QCA_CHG} libqca.dylib)
    ENDIF ()
ELSEIF (ISFW AND EXISTS "@QCA_LIBRARY@")
    # framework
    MESSAGE (STATUS "Copying QCA framework and updating library paths...")
    SET (QCA_LIBRARY "@QCA_LIBRARY@")
    STRING (REGEX MATCH "qca-qt5" _has_long_name "${QCA_LIBRARY}")
    IF (_has_long_name)
        SET (_qca_libname "qca-qt5")
    ELSE ()
        SET (_qca_libname "qca")
    ENDIF ()
    STRING (REGEX MATCH "${_qca_libname}.framework/${_qca_libname}" _has_bin "${QCA_LIBRARY}")
    IF (NOT _has_bin)
        SET (QCA_LIBRARY "${QCA_LIBRARY}/${_qca_libname}")
    ENDIF ()
    SET (QCA_CHG_TO "${ATLOADER}/@QGIS_PLUGIN_SUBDIR_REV@/${QGIS_FW_SUBDIR}/${_qca_libname}.framework/${_qca_libname}")
    IF (NOT IS_DIRECTORY "${QFWDIR}/${_qca_libname}.framework")
        STRING(REGEX REPLACE "/${_qca_libname}.framework.*" "" QCA_PARENT "@QCA_LIBRARY@")
        COPY_FRAMEWORK("${QCA_PARENT}" "${_qca_libname}" "${QFWDIR}")
        EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QFWDIR}/${_qca_libname}.framework/${_qca_libname}")
    ENDIF ()
    GET_INSTALL_NAME ("${QCA_LIBRARY}" "${_qca_libname}" QCA_CHG)
    IF (QCA_CHG)
        UPDATEQGISPATHS (${QCA_CHG} ${_qca_libname})
    ENDIF ()
ENDIF ()
# copy and relink the plugins
EXECUTE_PROCESS (COMMAND mkdir -p "${QPLUGDIR}/../crypto")
FIND_QCA_PLUGIN_DIR (1)
MESSAGE (STATUS "Updating QCA plugins with QCA library path in ${QCA_PLUGIN_DIR} ...")
# don't copy over any unneeded plugins
SET(QCA_PLUGINS logger ossl softstore)
FOREACH (qca_plugin ${QCA_PLUGINS})
   EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${QCA_PLUGIN_DIR}/crypto/libqca-${qca_plugin}.dylib" "${QPLUGDIR}/../crypto/")
   EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QCA_PLUGIN_DIR}/crypto/libqca-${qca_plugin}.dylib")
   IF (QCA_CHG)
      INSTALLNAMETOOL_CHANGE ("${QCA_CHG}" "${QCA_CHG_TO}" "${QPLUGDIR}/../crypto/libqca-${qca_plugin}.dylib")
   ENDIF ()
ENDFOREACH ()

# QSpatialite Qt plugin
# linked to qca and qgis_core frameworks (see also 2lib.cmake.in)
IF (@WITH_QSPATIALITE@ AND EXISTS "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
    # qca.framework
    EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
    INSTALLNAMETOOL_CHANGE ("${QCA_CHG}" "${QCA_CHG_TO}" "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
    # qgis_core.framework
    GET_INSTALL_NAME ("@QGIS_OUTPUT_DIRECTORY@/lib/qgis_core.framework/qgis_core" qgis_core.framework QGCORE)
    SET (QGCORE_CHG_TO "${ATLOADER}/@QGIS_PLUGIN_SUBDIR_REV@/${QGIS_FW_SUBDIR}/qgis_core.framework/qgis_core")
    INSTALLNAMETOOL_CHANGE ("${QGCORE}" "${QGCORE_CHG_TO}" "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
ENDIF ()

# QScintilla2

IF (@QSCINTILLA_LIBRARY@ MATCHES ".*libqscintilla2_qt5.*dylib")
    SET (QSCI_LIB "libqscintilla2_qt5")
ELSEIF (@QSCINTILLA_LIBRARY@ MATCHES ".*libqscintilla2.*dylib")
    SET (QSCI_LIB "libqscintilla2")
ENDIF ()
IF (QSCI_LIB)
    MESSAGE (STATUS "Copying QScintilla2 library and updating library paths...")
    EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QSCINTILLA_LIBRARY@" "${QLIBDIR}/${QSCI_LIB}.dylib")
    EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QLIBDIR}/${QSCI_LIB}.dylib")
    GET_INSTALL_NAME ("@QSCINTILLA_LIBRARY@" "${QSCI_LIB}" QSCI_CHG)
    IF (QSCI_CHG)
        UPDATEQGISPATHS (${QSCI_CHG} ${QSCI_LIB}.dylib)
    ENDIF (QSCI_CHG)
ENDIF ()

# QtKeychain

IF (@QTKEYCHAIN_LIBRARY@ MATCHES ".*libqt5keychain.dylib")
    MESSAGE (STATUS "Copying QtKeychain library and updating library paths...")
    EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@QTKEYCHAIN_LIBRARY@" "${QLIBDIR}/libqt5keychain.dylib")
    EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QLIBDIR}/libqt5keychain.dylib")
    GET_INSTALL_NAME ("@QTKEYCHAIN_LIBRARY@" "libqt5keychain" QTKEY_CHG)
    IF (QTKEY_CHG)
        UPDATEQGISPATHS (${QTKEY_CHG} libqt5keychain.dylib)
    ENDIF (QTKEY_CHG)
ENDIF ()

# Tell user what Python paths are being searched for modules and packages
# see: PYTHON_MODULE_PATHS in 0vars.cmake.in for why not all PyQt-related modules
#      can be assumed to exist in symlinked-resolved PyQt or sip module directories
MESSAGE (STATUS "PYTHON_MODULE_PATHS to be searched:")
FOREACH (PYPATH ${PYTHON_MODULE_PATHS})
    MESSAGE (STATUS "  ${PYPATH}")
ENDFOREACH (PYPATH)

# sip and PyQt

MESSAGE (STATUS "Copying sip...")
PYTHONMODULEDIR("sip.so" SIPMODDIR)
IF (SIPMODDIR)
    IF (NOT EXISTS "${QGISPYDIR}/sip.so" AND NOT EXISTS "${QGISPYDIR}/PyQt5/sip.so")
        # MYMESSAGE ("ditto ${QARCHS} \"${SIPMODDIR}/sip.so\" \"${QGISPYDIR}/\"")
        IF (${SIPMODDIR} MATCHES ".*PyQt5.*")
            EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${SIPMODDIR}/sip.so" "${QGISPYDIR}/PyQt5/")
            EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QGISPYDIR}/PyQt5/sip.so")
        ELSE ()
            EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${SIPMODDIR}/sip.so" "${QGISPYDIR}/")
            EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QGISPYDIR}/sip.so")
        ENDIF ()
        EXECUTE_PROCESS (COMMAND cp -fp "${SIPMODDIR}/sipconfig.py" "${QGISPYDIR}/")
    ENDIF ()
ELSE ()
    MESSAGE (STATUS "  sip module not found")
ENDIF ()

MESSAGE (STATUS "Copying PyQt...")
EXECUTE_PROCESS (COMMAND mkdir -p "${QGISPYDIR}/PyQt${QT_FWVER}")
FOREACH (PQ ${PYQTLIST})
    SET (MODNAME "${PQ}.so")
    SET (MODPYI "PyQt${QT_FWVER}/${PQ}.pyi")
    SET (MODSUBPATH "PyQt${QT_FWVER}/${MODNAME}")
    # search for each module separately, instead of only in first found PyQt directory, since PyQt may
    #  be installed to its a specific prefix, like with Homebrew, then symlinked into common 'site-packages'
    PYTHONMODULEDIR("${MODSUBPATH}" MODDIR)
    IF (MODDIR)
        IF (NOT EXISTS "${QGISPYDIR}/${MODSUBPATH}")
            # MESSAGE (STATUS "ditto ${QARCHS} \"${PYQT5MOD}\" \"${QGISPYDIR}/${MODSUBPATH}\"")
            EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${MODDIR}/${MODSUBPATH}" "${QGISPYDIR}/${MODSUBPATH}")
            EXECUTE_PROCESS (COMMAND install_name_tool -delete_rpath "@QT_LIBRARY_DIR@" "${QGISPYDIR}/${MODSUBPATH}")
            IF (EXISTS "${MODDIR}/${MODPYI}")
                EXECUTE_PROCESS (COMMAND ditto "${MODDIR}/${MODPYI}" "${QGISPYDIR}/${MODPYI}")
            ENDIF ()
        ENDIF ()
    ELSE (MODDIR)
        MESSAGE (STATUS "  PyQt5 module ${MODNAME} not found")
    ENDIF (MODDIR)
    UNSET(PYQT5MOD)
ENDFOREACH (PQ)
PYTHONMODULEDIR("PyQt${QT_FWVER}" PYQTMODDIR)
FILE (GLOB PQPYLIST "${PYQTMODDIR}/PyQt${QT_FWVER}/*.py*")
FOREACH (PQPY ${PQPYLIST})
    EXECUTE_PROCESS (COMMAND cp -fp "${PQPY}" "${QGISPYDIR}/PyQt${QT_FWVER}/")
ENDFOREACH (PQPY)
EXECUTE_PROCESS (COMMAND cp -RfpL "${PYQTMODDIR}/PyQt${QT_FWVER}/uic" "${QGISPYDIR}/PyQt${QT_FWVER}")

# PyQt utilities

FOREACH (PU pylupdate5;pyrcc5)
    IF (NOT EXISTS "${QBINDIR}/${PU}")
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "@PYQT5_BIN_DIR@/${PU}" "${QBINDIR}/")
    ENDIF ()
ENDFOREACH (PU)
IF (NOT EXISTS "${QBINDIR}/pyuic5")
    EXECUTE_PROCESS (COMMAND sed -E "s, /.*/PyQt5/uic/pyuic.py, \"\$(/usr/bin/dirname \"\$0\")/../../Resources/python/PyQt5/uic/pyuic.py\"," "@PYQT5_BIN_DIR@/pyuic5"
                     OUTPUT_VARIABLE PYUIC_CONTENTS)
    FILE (WRITE "${QBINDIR}/pyuic4" "${PYUIC_CONTENTS}")
    EXECUTE_PROCESS (COMMAND chmod +x "${QBINDIR}/pyuic4")
ENDIF ()

# PyQwt
# only if it's available, not compatible with newer PyQt
PYTHONMODULEDIR("Qwt5/_iqt.so" QWT4MODDIR)
IF (QWT4MODDIR)
    MESSAGE (STATUS "Copying PyQwt and updating library paths...")
    EXECUTE_PROCESS (COMMAND mkdir -p "${QGISPYDIR}/PyQt${QT_FWVER}/Qwt5")
    IF (NOT EXISTS "${QGISPYDIR}/PyQt${QT_FWVER}/Qwt5/_iqt.so")
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${QWT4MODDIR}/Qwt5/_iqt.so" "${QGISPYDIR}/PyQt${QT_FWVER}/Qwt5/")
    ENDIF ()
    IF (NOT EXISTS "${QGISPYDIR}/PyQt${QT_FWVER}/Qwt5/Qwt.so")
        EXECUTE_PROCESS (COMMAND ditto ${QARCHS} "${QWT4MODDIR}/Qwt5/Qwt.so" "${QGISPYDIR}/PyQt${QT_FWVER}/Qwt5/")
    ENDIF ()
    FILE (GLOB PQWPYLIST "${QWT4MODDIR}/Qwt5/*.py")
    FOREACH (PQWPY ${PQWPYLIST})
        EXECUTE_PROCESS (COMMAND cp -fp "${PQWPY}" "${QGISPYDIR}/PyQt${QT_FWVER}/Qwt5/")
    ENDFOREACH (PQWPY)
    IF (QWT_CHG)
        SET (QWT_CHG_TO "${ATLOADER}/../../../@QGIS_DATA_SUBDIR_REV@/@QGIS_LIB_SUBDIR@/libqwt.dylib")
        FOREACH (PW _iqt;Qwt)
            INSTALLNAMETOOL_CHANGE ("${QWT_CHG}" "${QWT_CHG_TO}" "${QGISPYDIR}/PyQt${QT_FWVER}/Qwt5/${PW}.so")
        ENDFOREACH (PW)
    ENDIF (QWT_CHG)
ENDIF ()

# QScintilla Python module
# should have already been copied with PyQt modules

IF (EXISTS "${QGISPYDIR}/PyQt${QT_FWVER}/Qsci.so")
    MESSAGE (STATUS "Updating QScintilla2 Python module library paths...")
	GET_INSTALL_NAME ("${QGISPYDIR}/PyQt${QT_FWVER}/Qsci.so" "${QSCI_LIB}" QSCI_CHG)
    IF (QSCI_CHG)
        SET (QSCI_CHG_TO "${ATEXECUTABLE}/@QGIS_LIB_SUBDIR@/${QSCI_LIB}.dylib")
        SET (QSCI_CHG_TO "${ATLOADER}/../../@QGIS_DATA_SUBDIR_REV@/@QGIS_LIB_SUBDIR@/${QSCI_LIB}.dylib")
        INSTALLNAMETOOL_CHANGE ("${QSCI_CHG}" "${QSCI_CHG_TO}" "${QGISPYDIR}/PyQt${QT_FWVER}/Qsci.so")
    ENDIF (QSCI_CHG)
ENDIF ()

# don't load plugins from system-installed Qt
FILE (WRITE "${QAPPDIRC}/Resources/qt.conf" "")
FOREACH (QA ${QGAPPLIST})
    FILE (WRITE "${QBINDIR}/${QA}.app/Contents/Resources/qt.conf" "")
ENDFOREACH (QA)

### update lib paths

MESSAGE (STATUS "Updating Qt library paths...")

FOREACH (QFW ${QTLISTQG})
    # get install names from installed in case bundled copy already changed
    # from a previous install attempt
    GET_INSTALL_NAME ("${QT_LIBRARY_DIR}/${QFW}.framework/${QFW}" ${QFW}.framework QQ)
    SET (QFW_CHG "${QQ}")
    # qgis stuff
    UPDATEQGISPATHS ("${QFW_CHG}" ${QFW})

    SET (LIBPOST "${QFW}.framework/${QFW}")
    # Qwt
    STRING (REGEX MATCH "\\.dylib$" ISLIB "@QWT_LIBRARY@")
    STRING (REGEX MATCH "qwt.framework" ISFW "@QWT_LIBRARY@")
    IF (ISLIB)
        SET (QFW_CHG_TO "${ATLOADER}/${QGIS_LIB_SUBDIR_REV}/${QGIS_FW_SUBDIR}/${LIBPOST}")
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QLIBDIR}/libqwt.dylib")
    ELSEIF (ISFW AND EXISTS "${QFWDIR}/qwt.framework/qwt")
        SET (QFW_CHG_TO "${ATLOADER}/../../../${LIBPOST}")
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QFWDIR}/qwt.framework/qwt")
    ENDIF (ISLIB)
    # qtkeychain
    STRING (REGEX MATCH "\\.dylib$" ISLIB "@QTKEYCHAIN_LIBRARY@")
    STRING (REGEX MATCH "qt5keychain.framework" ISFW "@QTKEYCHAIN_LIBRARY@")
    IF (ISLIB)
        SET (QFW_CHG_TO "${ATLOADER}/${QGIS_LIB_SUBDIR_REV}/${QGIS_FW_SUBDIR}/${LIBPOST}")
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QLIBDIR}/libqt5keychain.dylib")
    ELSEIF (ISFW AND EXISTS "${QFWDIR}/qt5keychain.framework/qt5keychain")
        SET (QFW_CHG_TO "${ATLOADER}/../../../${LIBPOST}")
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QFWDIR}/qt5keychain.framework/qt5keychain")
    ENDIF (ISLIB)
    # QwtPolar
    STRING (REGEX MATCH "\\.dylib$" ISLIB "@QWTPOLAR_LIBRARY@")
    STRING (REGEX MATCH "qwtpolar.framework" ISFW "@QWTPOLAR_LIBRARY@")
    IF (ISLIB)
        SET (QFW_CHG_TO "${ATLOADER}/${QGIS_LIB_SUBDIR_REV}/${QGIS_FW_SUBDIR}/${LIBPOST}")
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QLIBDIR}/libqwtpolar.dylib")
    ELSEIF (ISFW AND EXISTS "${QFWDIR}/qwtpolar.framework/qwtpolar")
        SET (QFW_CHG_TO "${ATLOADER}/../../../${LIBPOST}")
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QFWDIR}/qwtpolar.framework/qwtpolar")
    ENDIF (ISLIB)
    # QCA
    STRING (REGEX MATCH "\\.dylib$" ISLIB "@QCA_LIBRARY@")
    STRING (REGEX MATCH "${_qca_libname}.framework" ISFW "@QCA_LIBRARY@")
    IF (ISLIB)
        SET (QFW_CHG_TO "${ATLOADER}/${QGIS_LIB_SUBDIR_REV}/${QGIS_FW_SUBDIR}/${LIBPOST}")
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QLIBDIR}/libqca.dylib")
    ELSEIF (ISFW AND EXISTS "${QFWDIR}/${_qca_libname}.framework/${_qca_libname}")
        SET (QFW_CHG_TO "${ATLOADER}/../../../${LIBPOST}")
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QFWDIR}/${_qca_libname}.framework/${_qca_libname}")
    ENDIF (ISLIB)
    # QScintilla2
    SET (QFW_CHG_TO "${ATLOADER}/${QGIS_LIB_SUBDIR_REV}/${QGIS_FW_SUBDIR}/${LIBPOST}")
    INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QLIBDIR}/${QSCI_LIB}.dylib")
    # qt plugs
    SET (QFW_CHG_TO "${ATLOADER}/@QGIS_PLUGIN_SUBDIR_REV@/${QGIS_FW_SUBDIR}/${LIBPOST}")
    # qca plugins
    FOREACH (qca_plugin ${QCA_PLUGINS})
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QPLUGDIR}/../crypto/libqca-${qca_plugin}.dylib")
    ENDFOREACH ()
    FOREACH (QI qgif;qico;qjpeg;qsvg;qtiff)
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QPLUGDIR}/../imageformats/lib${QI}.dylib")
    ENDFOREACH (QI)
    FOREACH (QP cocoa;minimal;offscreen)
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QPLUGDIR}/../platforms/libq${QP}.dylib")
    ENDFOREACH (QP)
    FOREACH (QSL ${QTLISTSQL})
        IF (EXISTS "${QPLUGDIR}/../sqldrivers/libqsql${QSL}.dylib")
            INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QPLUGDIR}/../sqldrivers/libqsql${QSL}.dylib")
        ENDIF ()
    ENDFOREACH (QSL)
    IF (@WITH_QSPATIALITE@ AND EXISTS "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
        EXECUTE_PROCESS (COMMAND install_name_tool -id "libqsqlspatialite.dylib" "${QPLUGDIR}/../sqldrivers/libqsqlspatialite.dylib")
    ENDIF ()
    INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QPLUGDIR}/../iconengines/libqsvgicon.dylib")
    INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QPLUGDIR}/../phonon_backend/libphonon_qt7.dylib")
    INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QPLUGDIR}/../styles/libqmacstyle.dylib")
    # quick plugin
    SET (QFW_CHG_TO "${ATLOADER}/../../../${LIBPOST}")
    INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QAPPDIR}/qml/QgsQuick/libqgis_quick_plugin.dylib")
    # qt fw
    SET (QFW_CHG_TO "${ATLOADER}/../../../${LIBPOST}")
    FOREACH (QF ${QTLISTQG})
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QFWDIR}/${QF}.framework/${QF}")
    ENDFOREACH (QF)
    # PyQt (includes QScintilla2 module)
    SET (QFW_CHG_TO "${ATLOADER}/../../@QGIS_DATA_SUBDIR_REV@/${QGIS_FW_SUBDIR}/${LIBPOST}")
    FOREACH (PQ ${PYQTLIST})
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QGISPYDIR}/PyQt${QT_FWVER}/${PQ}.so")
    ENDFOREACH (PQ)
    # PyQwt
    SET (QFW_CHG_TO "${ATLOADER}/../../../@QGIS_DATA_SUBDIR_REV@/${QGIS_FW_SUBDIR}/${LIBPOST}")
    FOREACH (PW _iqt;Qwt)
        IF (EXISTS "${QGISPYDIR}/PyQt${QT_FWVER}/Qwt5/${PW}.so")
            INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QGISPYDIR}/PyQt${QT_FWVER}/Qwt5/${PW}.so")
        ENDIF ()
    ENDFOREACH (PW)
    # bin - PyQt utils
    SET (QFW_CHG_TO "${ATEXECUTABLE}/@QGIS_BIN_SUBDIR_REV@/${QGIS_FW_SUBDIR}/${LIBPOST}")
    FOREACH (PB pylupdate4;pyrcc4)
        INSTALLNAMETOOL_CHANGE ("${QFW_CHG}" "${QFW_CHG_TO}" "${QBINDIR}/${PB}")
    ENDFOREACH (PB)
ENDFOREACH (QFW)
